/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plash.display;

import java.util.ArrayList;
import plash.geom.Point;

/**
 *
 * @author User
 */
public class GraphicCurvePathShape extends GraphicLinearPathShape
{
    private ArrayList<Point> _controls;
    
    public GraphicCurvePathShape(Graphics g)
    {
        super(g);
        _controls = new ArrayList<Point>();
    }
    
    public void addControlPoint(Point control)
    {
        _controls.add(control);
        
        __minPoint.setX(Math.min(control.getX(), __minPoint.getX()));
        __minPoint.setY(Math.min(control.getY(), __minPoint.getX()));
        
        __maxPoint.setX(Math.max(control.getX(), __maxPoint.getX()));
        __maxPoint.setY(Math.max(control.getY(), __maxPoint.getX()));
        
        __boundingBox.setX(__minPoint.getX());
        __boundingBox.setY(__minPoint.getY());
        
        __boundingBox.setWidth(__maxPoint.getX() - __minPoint.getY());
        __boundingBox.setHeight(__maxPoint.getY() - __minPoint.getY());
    }

    @Override
    public void execute()
    {
        if(__g == null)
        {
            return;
        }
        if(__painter == null)
        {
            __painter = __g.getPainter();
        }
        if(__points.size() < 1)
        {
            return;
        }
        
        __g.getOwner().getPainter().beginShape();
        
        for(int i=0;i<__points.size()-1;i++)
        {
            Point controlPoint = _controls.get(i);
            Point firstPoint = __points.get(i);
            Point secondPoint = __points.get(i+1);
            
            __g.getOwner().getPainter().curve(
                            controlPoint.getX(), controlPoint.getY(), 
                            firstPoint.getX(), firstPoint.getY(), 
                            secondPoint.getX(), secondPoint.getY(), 
                            controlPoint.getX(), controlPoint.getY());
        }
        __g.getOwner().getPainter().endShape();
    }
}
